package com.gnerv.sylvanas.business.manage.logic;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.framework.core.BaseUkIdGenerate;
import com.gnerv.sylvanas.business.manage.model.bo.ApiBO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiCreateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiRoleDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiUpdateDTO;
import com.gnerv.sylvanas.business.manage.operation.ApiOperationService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class ApiLogicService  {

    private final ApiOperationService apiOperationService;
    private final BaseUkIdGenerate baseUkIdGenerate;

    
    public RestResult<Void> create(ApiCreateDTO apiCreateDTO) {
        ApiBO apiBO = apiCreateDTO.toApiBO();
        apiBO.setUkId(baseUkIdGenerate.generate());
        apiOperationService.create(apiBO);
        return RestResult.ok();
    }

    
    public RestResult<Void> deleteByUkId(String ukId) {
        apiOperationService.deleteByUkId(ukId);
        apiOperationService.unbindRole(ukId);
        return RestResult.ok();
    }

    
    public RestResult<Void> updateByUkId(ApiUpdateDTO apiUpdateDTO) {
        ApiBO apiBO = apiUpdateDTO.toApiBO();
        apiOperationService.updateByUkId(apiBO);
        return RestResult.ok();
    }

    
    public RestResult<ApiDTO> selectByUkId(String ukId) {
        ApiDTO apiDTO = apiOperationService.selectByUkId(ukId);
        return RestResult.ok(apiDTO);
    }

    
    public RestResult<List<ApiDTO>> selectListByName(String name, int page, int size) {
        List<ApiDTO> apiDTOList = apiOperationService.selectListByName(name, page, size);
        int count = apiOperationService.selectCountByName(name);
        return RestResult.ok(apiDTOList).page(count, page, size);
    }

    
    public RestResult<List<ApiDTO>> selectListByRoleUkId(String roleUkId) {
        List<ApiDTO> apiDTOList = apiOperationService.selectListByRoleUkId(roleUkId);
        return RestResult.ok(apiDTOList);
    }

    
    public RestResult<Void> bindRole(ApiRoleDTO apiRoleDTO) {
        apiOperationService.unbindRole(apiRoleDTO.getApiUkId());
        apiOperationService.bindRole(apiRoleDTO.getApiUkId(), apiRoleDTO.getRoleUkIdList());
        return RestResult.ok();
    }

    
    public RestResult<Void> unbindRole(String apiUkId) {
        apiOperationService.unbindRole(apiUkId);
        return RestResult.ok();
    }

    
    public RestResult<List<ApiDTO>> selectListByGroupUkId(String groupUkId, int page, int size) {
        List<ApiDTO> apiDTOList = apiOperationService.selectListByGroupUkId(groupUkId, page, size);
        return RestResult.ok(apiDTOList).page(page, size, size);
    }

}
